//index.js
//获取应用实例
const app = getApp()
const utils = require('../../utils/util.js')
Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        index: 0,
        array: '',
        userid: wx.getStorageSync('userOpenId'),
        accesstoken: '',
        name: '',
        passwd: '',
        type: false,
        listtype: false,
        winWidth: '',
        text: "请选择"
    },
    //事件处理函数
    bindViewTap: function() {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    goTojc: function() {
        wx.navigateTo({
            url: '../info/logs'
        })
    },
    gotoAdd: function() {
        wx.navigateTo({
            url: '../add/index'
        })
    },
    imageload: function(e) {
        // console.log(e)
    },
    formSubmit: function(e) {
        let that = this
            // // console.log(e)
        let types = that.data.type
        if (types) {
            wx.request({
                url: 'https://passwd.daoker.club/api/sendMessage',
                method: 'post',
                header: { "Content-Type": "application/x-www-form-urlencoded" },
                data: {
                    touser: wx.getStorageSync('userOpenId'),
                    template_id: 'lIs9qOYpYgpurP6nWfF_WxBNl3k0LAEEiMTMSkKLivU',
                    form_id: e.detail.formId,
                    key1: that.data.passwd,
                    key2: that.data.name,
                    key3: new Date()

                },
                success: function(datas) {
                    // // console.log(datas)
                    // // console.log(that.data.passwd)
                    // console.log(datas)
                    wx.showModal({
                        title: '成功提示',
                        content: '您的密码已发送，请在服务通知中查看！',
                        showCancel: false,
                        success: function(res) {
                            if (res.confirm) {
                                // console.log('用户点击确定')
                            }
                        }
                    })

                }
            })
        } else {
            // console.log("no")
            wx.showModal({
                title: '温馨提示',
                content: '您还没有选择要查看的密码，请选择！',
                showCancel: false,
                success: function(res) {
                    if (res.confirm) {
                        // console.log('用户点击确定')
                    }
                }
            })
        }
    },
    bindPickerChange: function(e) {

        let id = e.currentTarget.dataset.value[e.detail.value].id
        let name = e.currentTarget.dataset.value[e.detail.value].name


        wx.request({
            url: 'https://passwd.daoker.club/api/showPass',
            method: 'get',
            data: {
                userid: wx.getStorageSync('userOpenId'),
                id: e.currentTarget.dataset.value[e.detail.value].id
            },
            success: data => {
                // console.log(data.data.data.key, name)
                this.setData({
                    name: name,
                    passwd: data.data.data.key,
                    type: true,
                    text: name + '的密码'
                })
            }

        })


    },
    onShow: function() {
        let that = this
        setTimeout(function() {
            utils.getUserPassAll(wx.getStorageSync('userOpenId'), (res) => {
                // console.log(res)
                let list = res.data.data.list
                    // console.log(list)
                if (list != "") {
                    that.setData({
                        array: res.data.data.list,
                        listtype: true
                    })
                }
            })
        }, 100)


    },
    onLoad: function() {
        let that = this
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
            utils.getUserPassAll(wx.getStorageSync('userOpenId'), (res) => {
                // console.log(res)
                let list = res.data.data.list
                    // console.log(list)
                if (list != "") {
                    that.setData({
                        array: res.data.data.list,
                        listtype: true
                    })
                }
                let name = this.data.userInfo.nickName
                let avatarurl = this.data.userInfo.avatarUrl
                utils.getLogin(name, avatarurl)
            })
        } else if (this.data.canIUse) {
            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
            // 所以此处加入 callback 以防止这种情况
            app.userInfoReadyCallback = res => {
                this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                })
                utils.getUserPassAll(wx.getStorageSync('userOpenId'), (res) => {
                    // console.log(res)
                    let list = res.data.data.list
                        // console.log(list)
                    if (list != "") {
                        that.setData({
                            array: res.data.data.list,
                            listtype: true
                        })
                    }
                    let s = that.data
                    let name = this.data.userInfo.nickName
                    let avatarurl = this.data.userInfo.avatarUrl
                    utils.getLogin(name, avatarurl)
                        // console.log(this.data.userInfo.nickName)
                        // console.log(this.data.userInfo.avatarUrl)
                })
            }

        } else {
            // 在没有 open-type=getUserInfo 版本的兼容处理
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                    utils.getUserPassAll(wx.getStorageSync('userOpenId'), (res) => {
                        // console.log(res)
                        let list = res.data.data.list
                            // console.log(list)
                        if (list != "") {
                            that.setData({
                                array: res.data.data.list,
                                listtype: true
                            })
                        }
                        let name = this.data.userInfo.nickName
                        let avatarurl = this.data.userInfo.avatarUrl
                        utils.getLogin(name, avatarurl)
                    })
                }
            })
        }
        utils.getUserPassAll(wx.getStorageSync('userOpenId'), (res) => {
            // console.log(res)
            let list = res.data.data.list
                // console.log(list)
            if (list != "") {
                that.setData({
                    array: res.data.data.list,
                    listtype: true
                })
            }
        })
        this.setData({
            winWidth: app.SystemInfo.screenWidth
        })

    },
    getUserInfo: function(e) {

        // console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
        utils.getUserPassAll(wx.getStorageSync('userOpenId'), (res) => {
            let list = res.data.data.list
            if (list != "") {
                this.setData({
                    array: res.data.data.list,
                    listtype: true
                })
            }

            let name = this.data.userInfo.nickName
            let avatarurl = this.data.userInfo.avatarUrl
            utils.getLogin(name, avatarurl)
        })

    },
    onShareAppMessage: function(res) {

        return {
            title: '密钥大师|以后再也不用担心忘记密码啦！',
            path: '/pages/index/index',
            imageUrl: '/image/fxbg.jpg'
        }
    }
})